import sys
import os

fBegin = 0x3a4
fSize = 32 * 1024 * 1024
fSkip = 1
cBegin = 0xcc


def calculate_checksum(in_file):
    with open(in_file, 'rb+') as file:
        file.seek(fBegin)
        data = file.read(fSize)
        checksum = 0
        for x in range(0, len(data), 4 * fSkip):
            checksum = checksum + int.from_bytes(data[x:x + 4], 'little')
            checksum = checksum & 0xFFFFFFFF
        checksum = ((checksum >> 16) & 0xFFFF) + checksum
        checksum &= 0xFFFF
        checksum ^= 0xFEDC
        print("checksum: " + str(checksum))
        file.seek(cBegin)
        file.write(int.to_bytes(checksum, 2, 'little'))


if __name__ == "__main__":
    calculate_checksum(sys.argv[1])
